package com.tangyuan.common.util;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.tangyuan.common.config.AliyunStsOSSConfig;
import lombok.extern.slf4j.Slf4j;



@Slf4j
public class AliyunStsOSSUtils {

    public static AssumeRoleResponse createOssToken() {

        try {
            DefaultProfile.addEndpoint("", "", "Sts", "sts.aliyuncs.com");
            IClientProfile profile = DefaultProfile.getProfile("", AliyunStsOSSConfig.accessKeyId, AliyunStsOSSConfig.accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysProtocol(ProtocolType.HTTPS);
            request.setDurationSeconds(900L);
            request.setRoleArn(AliyunStsOSSConfig.roleArn);
            request.setRoleSessionName(AliyunStsOSSConfig.roleSessionName);
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            log.error("创建阿里云 OSS 临时token异常", e);
        }

        return null;
    }
}
